﻿
using System;
using System.Collections.Generic;
using System.Data;
using QLKS.Entity;

namespace QLKS.DAL
{
    public class DMNguoiDungDAO
    {
       private SqlDataProvider dataProvider;

       public DMNguoiDungDAO()
        {
            dataProvider = new SqlDataProvider();
        }

       public DMNguoiDung CheckLogin(DMNguoiDung obj)
       {
           DMNguoiDung nguoiDung = new DMNguoiDung(); ;
           try
           {
               DataTable dt = dataProvider.ExecuteDataTable("sp_DMNguoiDung_CheckLogin"
                   , new string[] { "MaNguoiDung", "TenNguoiDung", "MatKhau", "HoTen", "Email", "DienThoai", "DiaChi", "GhiChu" }
                   , new object[] { obj.MaNguoiDung, obj.TenNguoiDung, obj.MatKhau, obj.HoTen, obj.Email, obj.DienThoai, obj.DiaChi, obj.GhiChu }
               );
               if (dt.Rows.Count > 0)
               {
                   nguoiDung = new DMNguoiDung();
                   nguoiDung.MaNguoiDung = Convert.ToInt32(dt.Rows[0]["MaNguoiDung"]);
                   nguoiDung.TenNguoiDung = dt.Rows[0]["TenNguoiDung"] is DBNull ? string.Empty : dt.Rows[0]["TenNguoiDung"].ToString();
                   //nguoiDung.MatKhau = dt.Rows[0]["MatKhau"] is DBNull ? string.Empty : dt.Rows[0]["MatKhau"].ToString();
                   nguoiDung.HoTen = dt.Rows[0]["HoTen"] is DBNull ? string.Empty : dt.Rows[0]["HoTen"].ToString();
                   nguoiDung.Email = dt.Rows[0]["Email"] is DBNull ? string.Empty : dt.Rows[0]["Email"].ToString();
                   nguoiDung.DienThoai = dt.Rows[0]["DienThoai"] is DBNull ? string.Empty : dt.Rows[0]["DienThoai"].ToString();
                   nguoiDung.DiaChi = dt.Rows[0]["DiaChi"] is DBNull ? string.Empty : dt.Rows[0]["DiaChi"].ToString();
                   nguoiDung.GhiChu = dt.Rows[0]["GhiChu"] is DBNull ? string.Empty : dt.Rows[0]["GhiChu"].ToString();
               }
               else {
                   nguoiDung = null; 
               }
           }
           catch (Exception) { nguoiDung = null; }

           return nguoiDung;
       }

        public List<DMNguoiDung> GetByAll(DMNguoiDung obj)
        {
            List<DMNguoiDung> lstData = new List<DMNguoiDung>();;
            try
            {
                DataTable dt = dataProvider.ExecuteDataTable("sp_DMNguoiDung_GetByAll"
                    , new string[] { "MaNguoiDung", "TenNguoiDung", "MatKhau", "HoTen", "Email", "DienThoai", "DiaChi", "GhiChu" }
                    , new object[] { obj.MaNguoiDung, obj.TenNguoiDung, obj.MatKhau, obj.HoTen, obj.Email, obj.DienThoai, obj.DiaChi, obj.GhiChu }
                );
                DMNguoiDung entity;
                foreach(DataRow myrow in dt.Rows)
                {
                    entity = new DMNguoiDung();
                    entity.MaNguoiDung = Convert.ToInt32(myrow["MaNguoiDung"]);
                    entity.TenNguoiDung = myrow["TenNguoiDung"] is DBNull ? string.Empty : myrow["TenNguoiDung"].ToString();
                    entity.MatKhau = myrow["MatKhau"] is DBNull ? string.Empty : myrow["MatKhau"].ToString();
                    entity.HoTen = myrow["HoTen"] is DBNull ? string.Empty : myrow["HoTen"].ToString();
                    entity.Email = myrow["Email"] is DBNull ? string.Empty : myrow["Email"].ToString();
                    entity.DienThoai = myrow["DienThoai"] is DBNull ? string.Empty : myrow["DienThoai"].ToString();
                    entity.DiaChi = myrow["DiaChi"] is DBNull ? string.Empty : myrow["DiaChi"].ToString();
                    entity.GhiChu = myrow["GhiChu"] is DBNull ? string.Empty : myrow["GhiChu"].ToString();
                    lstData.Add(entity);
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
            return lstData;
        }

        public bool Insert(DMNguoiDung obj)
        {
            int i = dataProvider.ExecuteNonQuery("sp_DMNguoiDung_Insert"
                        , new string[] { "MaNguoiDung", "TenNguoiDung", "MatKhau", "HoTen", "Email", "DienThoai", "DiaChi", "GhiChu" }
                        , new object[] { obj.MaNguoiDung, obj.TenNguoiDung, obj.MatKhau, obj.HoTen, obj.Email, obj.DienThoai, obj.DiaChi, obj.GhiChu }
                    );
            return i == 1 ? true : false;
        }

        public bool Update(DMNguoiDung obj)
        {
            int i = dataProvider.ExecuteNonQuery("sp_DMNguoiDung_Update"
                        , new string[] { "MaNguoiDung", "TenNguoiDung", "MatKhau", "HoTen", "Email", "DienThoai", "DiaChi", "GhiChu" }
                        , new object[] { obj.MaNguoiDung, obj.TenNguoiDung, obj.MatKhau, obj.HoTen, obj.Email, obj.DienThoai, obj.DiaChi, obj.GhiChu }
                    );
            return i == 1 ? true : false;
        }

        public bool Delete(DMNguoiDung obj)
        {
            int i = dataProvider.ExecuteNonQuery("sp_DMNguoiDung_Delete"
                        , new string[] { "MaNguoiDung", "TenNguoiDung", "MatKhau", "HoTen", "Email", "DienThoai", "DiaChi", "GhiChu" }
                        , new object[] { obj.MaNguoiDung, obj.TenNguoiDung, obj.MatKhau, obj.HoTen, obj.Email, obj.DienThoai, obj.DiaChi, obj.GhiChu }
                    );
            return i == 1 ? true : false;
        }
    }
}
